using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Autenticazione;

namespace BusinessObject
{
	public class Fights
	{
		public string EMail;
		public long Id;
		public string Iscrizioni;
		private int IdEvento;
		public Fights()
		{
			this.IdEvento = int.Parse(ConfigurationManager.AppSettings["IdEvento"]);
		}
		public Fights(string email, long id, string iscrizioni)
		{
			this.EMail = email;
			this.Id = id;
			this.Iscrizioni = iscrizioni;
			this.IdEvento = int.Parse(ConfigurationManager.AppSettings["IdEvento"]);
		}
		public DataTable GetTbFights(string email)
		{
			string strSQL = string.Format("select F.EMail, F.Id, F.Iscrizioni, A.FullName from Fights F inner join Atleta A on F.EMail = A.EMail and F.Id = A.Id where F.Email = '{0}' and IdEvento = {1} order by FullName", email, this.IdEvento.ToString());
			DB db = new DB();
			return db.GetTable(strSQL);
		}
		public void PopulateFights(string email)
		{
			List<Atleta> atleti = new List<Atleta>();
			string strSQL = string.Format("select A.Email, A.Id from Atleta A left outer join Fights F on F.EMail = A.EMail and F.Id = A.Id where F.Id is null and A.EMail = '{0}'", email);
			DB db = new DB();
			SqlDataReader dr = db.Reader(strSQL);
			if (dr == null)
			{
				return;
			}
			while (dr.Read())
			{
				atleti.Add(new Atleta
				{
					EMail = dr["EMail"].ToString(), 
					Id = long.Parse(dr["Id"].ToString())
				});
			}
			db.CloseConnection();
			foreach (Atleta at in atleti)
			{
				strSQL = string.Format("insert into Fights (EMail, Id, Iscrizioni, IdEvento) values ('{0}', {1}, '0000000000000000', {2})", at.EMail, at.Id.ToString(), this.IdEvento.ToString());
				db.ExecuteNonQuery(strSQL);
			}
		}
		public List<Fights> GetFights(string email)
		{
			List<Fights> fights = new List<Fights>();
			string strSQL = string.Format("select * from Fights where Email = '{0}' and IdEvento = {1} order by Id", email, this.IdEvento.ToString());
			DB db = new DB();
			SqlDataReader dr = db.Reader(strSQL);
			if (dr == null)
			{
				return fights;
			}
			while (dr.Read())
			{
				fights.Add(new Fights(dr["Email"].ToString(), long.Parse(dr["Id"].ToString()), dr["Iscrizioni"].ToString()));
			}
			db.CloseConnection();
			return fights;
		}
		public bool Update()
		{
			string strSQL = string.Format("update Fights set Iscrizioni = '{0}' where EMail = '{1}' and Id = {2} and IdEvento = {3}", new object[]
			{
				this.Iscrizioni, 
				this.EMail, 
				this.Id.ToString(), 
				this.IdEvento.ToString()
			});
			DB db = new DB();
			return db.ExecuteNonQuery(strSQL);
		}
		public bool DeleteFights(string email, long id)
		{
			string strSQL = string.Format("delete from Fights where Id = {0} and EMail = '{1}' and IdEvento = {2}", id.ToString(), email, this.IdEvento.ToString());
			DB db = new DB();
			return db.ExecuteNonQuery(strSQL);
		}
	}
}
